﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Legislation
{
    public partial class Legislation : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                //绑定法规库
                this.bindLegislationLibary();
                //绑定法规类
                this.bindLegislationClass();
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();
                this.bindStatusID();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        LegislationController.getController().doDelegeLegislation(ID);
                        break;
                }
            }
        }
        /// <summary>
        /// 绑定法规库
        /// </summary>
        public void bindLegislationLibary()
        {
            dropLegislationLibaryID.DataSource = LegislationController.getController().getLegislationLibaryList(100);
            dropLegislationLibaryID.DataValueField = "ID";
            dropLegislationLibaryID.DataTextField = "LegislationLibaryName";
            dropLegislationLibaryID.DataBind();
            dropLegislationLibaryID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定法规类
        /// </summary>
        public void bindLegislationClass()
        {
            DropLegislationClassID.DataSource = LegislationController.getController().getLegislationClassList(100);
            DropLegislationClassID.DataValueField = "ID";
            DropLegislationClassID.DataTextField = "ClassName";
            DropLegislationClassID.DataBind();
            DropLegislationClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }
        /// <summary>
        /// 绑定省份
        /// </summary>
        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 省份选定出发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }

        /// <summary>
        /// 绑定城市
        /// </summary>
        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定状态
        /// </summary>
        public void bindStatusID()
        {
            DropStatusID.Items.Insert(0, new ListItem("--请选择--", "0"));
            DropStatusID.Items.Insert(1, new ListItem("有效", "1"));
            DropStatusID.Items.Insert(2, new ListItem("失效", "2"));
            DropStatusID.Items.Insert(3, new ListItem("被修订", "3"));
            DropStatusID.Items.Insert(4, new ListItem("被修正", "4"));
        }

        /// <summary>
        /// 绑定需要修改的数据
        /// (就是点击修改某条记录，把上面的修改文本的信息，设置成你点的这条记录)
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.Legislation model = LegislationController.getController().getLegislationModel(ID);
            //输入框赋值
            dropLegislationLibaryID.SelectedValue = model.LegislationLibaryID.ToString();
            DropLegislationClassID.SelectedValue = model.LegislationClassID.ToString();
            DropProvinceID.SelectedValue = model.ProvinceID.ToString();
            DropCityID.SelectedValue = model.CityID.ToString();
            DropStatusID.SelectedValue = model.StatusID.ToString();
            tbTitle.Text = model.Title;
          //  tbAddtime.Text = model.Addtime.ToString();
            tbEffectivetime.Text = model.Effectivetime.ToString();
            tbContent.Text = model.Content;

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                LegislationController.getController().doModifyLegislation(int.Parse(IDS.Value),
                    dropLegislationLibaryID.SelectedValue,
                    DropLegislationClassID.SelectedValue,
                    int.Parse(DropProvinceID.SelectedValue),
                    int.Parse(DropCityID.SelectedValue),
                    int.Parse(DropStatusID.SelectedValue),
                    tbTitle.Text,
                    DateTime.Parse(tbEffectivetime.Text),
                    tbContent.Text);
            }
            else
            {
                LegislationController.getController().doAddLegislation(dropLegislationLibaryID.SelectedValue,
                    DropLegislationClassID.SelectedValue,
                      int.Parse(DropProvinceID.SelectedValue),
                      int.Parse(DropCityID.SelectedValue),
                      int.Parse(DropStatusID.SelectedValue),
                      tbTitle.Text,
                      DateTime.Parse(tbEffectivetime.Text),
                      tbContent.Text);
            }
        }
    }
}